package com.jmu.bishe.service;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jmu.bishe.dao.SimpleHibernateDao;
import com.jmu.bishe.entity.House;
import com.jmu.bishe.entity.Member;
import com.jmu.bishe.entity.News;
import com.jmu.bishe.utils.BeanUtils;
import com.jmu.bishe.utils.StringUtils;
import com.jmu.bishe.utils.UUIDGenerator;


@Service
@Transactional
public class HouseService {
	
	private static Logger logger = Logger.getLogger(HouseService.class);

	
	private SimpleHibernateDao<House, String> houseDao;
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		//初始化DAO
	
		houseDao = new SimpleHibernateDao<House, String>(sessionFactory, House.class);
	}

	

	
	private JdbcTemplate jdbcTemplate;
	
	@Resource(name = "jdbcTemplate")
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

   
    public Boolean saveOrUpdate(House house){
    	try{
    		if(house.getHid()==null){
    			house.setHid(UUIDGenerator.generate());
    		}
    		houseDao.saveOrUpdate(house);
    		return true;
    	}catch (Exception e) {
    		logger.info("保存房源异常",e);
    		return false;
    	}
    	
    	
    }
    
    public Map getParamMap(HttpServletRequest request){
        String sql = "select t.*,to_char(t.display_date,'yyyy-mm-dd') DISPLAYDATE from house t where 1=1 and t.status = 0  ";
        List<String> tjs = new ArrayList<String>();
        
        String dateType = request.getParameter ("dateType");
        if(!StringUtils.isBlank(dateType)){
        	if(dateType.equals("1")){
        		sql += " and t.ROOM_NUM =1 ";
        		}
        	if(dateType.equals("2")){
        		sql += " and t.ROOM_NUM =2 ";
        		}
        	if(dateType.equals("3")){
        		sql += " and t.ROOM_NUM =3 ";
        		}
        	if(dateType.equals("4")){
        		sql += " and t.ROOM_NUM =4 ";
        		}
        	if(dateType.equals("5")){
        		sql += " and t.PRICE <=50 ";
        		}
        	if(dateType.equals("6")){
        		sql += " and t.PRICE >50 ";
        		sql += " and t.PRICE <=100"; 
        		}
        	if(dateType.equals("7")){
        		sql += " and t.PRICE >100 ";
        		sql += " and t.PRICE <=200"; 
        		}
        	if(dateType.equals("8")){
        		sql += " and t.PRICE >200 ";
        		}
        			
        
        }else{
        	if(request.getParameter("room")!=null){
        		sql += " and t.ROOM_NUM =?  ";
        		tjs.add(request.getParameter("room"));
        	}
        	if(request.getParameter("parlor")!=null){
        		sql += " and t.PARLOR_NUM =?  ";
        		tjs.add(request.getParameter("parlor"));
        	}
        	if(request.getParameter("bathroom")!=null){
        		sql += " and t.BATHROOM_NUM =?   ";
        		tjs.add(request.getParameter("bathroom"));
        		
        	}
        	if(request.getParameter("start")!=null){
        		sql += " and t.price >= ? ";
        		tjs.add(request.getParameter("start"));
        	}
        	if(request.getParameter("end")!=null){
        		sql += " and t.price <= ? ";
        		tjs.add(request.getParameter("end"));
        	}
        }
        
        String keywords = request.getParameter("keywords");
        
        if(!StringUtils.isBlank(keywords)){
        	try {
    			keywords = URLDecoder.decode(keywords,"utf-8");
    		} catch (UnsupportedEncodingException e) {
    			logger.info("中文参数编码失败！",e);
    		}
            sql += "and T.ADDRESS like '%"+keywords+"%' ";
        }       
       
        sql += "  order by t.display_date desc ";
        Map map = new HashMap();
        map.put("sql",sql);
        map.put("tjs",tjs);
        return map;
    }
    public Map getRentParamMap(HttpServletRequest request){
        String sql = "select t.* ,to_char(t.display_date,'yyyy-mm-dd') DISPLAYDATE FROM house t where 1=1 and t.status = 1  ";
        List<String> tjs = new ArrayList<String>();
        
        String dateType = request.getParameter ("dateType");
        if(!StringUtils.isBlank(dateType)){
        	if(dateType.equals("1")){
        		sql += " and t.ROOM_NUM =1 ";
        		}
        	if(dateType.equals("2")){
        		sql += " and t.ROOM_NUM =2 ";
        		}
        	if(dateType.equals("3")){
        		sql += " and t.ROOM_NUM =3 ";
        		}
        	if(dateType.equals("4")){
        		sql += " and t.ROOM_NUM =4 ";
        		}
        	if(dateType.equals("5")){
        		sql += " and t.PRICE <=1000 ";
        		}
        	if(dateType.equals("6")){
        		sql += " and t.PRICE >1000 ";
        		sql += " and t.PRICE <=2000"; 
        		}
        	if(dateType.equals("7")){
        		sql += " and t.PRICE >2000 ";
        		sql += " and t.PRICE <=3000"; 
        		}
        	if(dateType.equals("8")){
        		sql += " and t.PRICE >3000 ";
        		}
        			
        
        }else{
        	if(request.getParameter("room")!=null){
        		sql += " and t.ROOM_NUM =?  ";
        		tjs.add(request.getParameter("room"));
        	}
        	if(request.getParameter("parlor")!=null){
        		sql += " and t.PARLOR_NUM =?  ";
        		tjs.add(request.getParameter("parlor"));
        	}
        	if(request.getParameter("bathroom")!=null){
        		sql += " and t.BATHROOM_NUM =?   ";
        		tjs.add(request.getParameter("bathroom"));
        		
        	}
        	if(request.getParameter("start")!=null){
        		sql += " and t.price >= ? ";
        		tjs.add(request.getParameter("start"));
        	}
        	if(request.getParameter("end")!=null){
        		sql += " and t.price <= ? ";
        		tjs.add(request.getParameter("end"));
        	}
        }
        
        String keywords = request.getParameter("keywords");
        
        if(!StringUtils.isBlank(keywords)){
        	try {
    			keywords = URLDecoder.decode(keywords,"utf-8");
    		} catch (UnsupportedEncodingException e) {
    			logger.info("中文参数编码失败！",e);
    		}
            sql += "and T.ADDRESS like '%"+keywords+"%' ";
        }       
       
        sql += "  order by t.display_date desc ";
        Map map = new HashMap();
        map.put("sql",sql);
        map.put("tjs",tjs);
        return map;
    }


	public House getHouse(String hid) {
		// TODO Auto-generated method stub
		House house = houseDao.get(hid);
		return house;
	}


	public Map getMyHouseParamMap(HttpServletRequest request, Member m) {
		// TODO Auto-generated method stub
		String sql = "";
		List<String> tjs = new ArrayList<String>();
		if("0".equals(m.getRole())){
    		//0是超级管理员，可以取到所有的新闻列表
    		sql = "select t.*,to_char(t.display_date,'yyyy-mm-dd') DISPLAYDATE  from house t where 1=1 ";
    	}else{
    		sql = "select t.*,to_char(t.display_date,'yyyy-mm-dd') DISPLAYDATE  from house t where user_id=? ";
    		 tjs.add(m.getUserId());
    	}
                
        String keywords = request.getParameter("keywords");
        
        if(!StringUtils.isBlank(keywords)){
        	try {
    			keywords = URLDecoder.decode(keywords,"utf-8");
    		} catch (UnsupportedEncodingException e) {
    			logger.info("中文参数编码失败！",e);
    		}
            sql += "AND T.ADDRESS like '%"+keywords+"%' ";
        }  
		return null;
        
	}

    public boolean checkedHouse(String ids){
    	String[] arr = ids.split(",");
    	for(String s : arr){    		
    		if(!StringUtils.isBlank(s)){
    			House temp = houseDao.get(s);
        		if(temp!=null){
        			temp.setStatus(1);
        		}
    		}
    	}
    	return true;
    }


	public boolean deleteHouse(String ids, String userId) {
		// TODO Auto-generated method stub
    	String[] arr = ids.split(",");
    	for(String s : arr){    		
    		if(!StringUtils.isBlank(s)){
    			House temp = houseDao.get(s);
        		if(temp!=null){
        			if(temp.getUserId().equals(userId)){
        				houseDao.delete(s);
        			}
        		}
    		}
    	}
    	return true;
	}
}